<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Run / RunWait</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<style type="text/css">
<!--
.style3 {color: #FF0000; font-weight: bold; }
-->
</style>
</head>
<body>

<h1>Run / RunWait</h1>

<p>Runs an external program. Unlike Run, RunWait will wait until
the program finishes before continuing.</p>

<pre class="Syntax">Run, Target [, WorkingDir, Max|Min|Hide|UseErrorLevel, OutputVarPID]</pre>
<h3>Parameters</h3>
<dl>

  <dt>Target</dt>
  <dd><p>A document, URL, executable file (.exe, .com, .bat, etc.), shortcut (.lnk), or <a href="#verbs">system verb</a> to launch (see remarks). If <em>Target</em> is a local file and no path was specified with it, <a href="../Variables.htm#WorkingDir">A_WorkingDir</a> will be searched first. If no matching file is found there, the system will search for and launch the file if it is integrated (&quot;known&quot;), e.g. by being contained in one of the PATH folders.</p>
      <p>To pass parameters, add them immediately after the program or document name. If a parameter contains spaces, it is  safest to enclose it in double quotes (even though it may work without them in some cases).</p></dd>

  <dt>WorkingDir</dt>
  <dd><p>The working directory for the launched item. Do not enclose the name in double quotes even if it contains spaces. If omitted, the script's own working directory (<a href="../Variables.htm#WorkingDir">A_WorkingDir</a>) will be used.</p></dd>

  <dt>Max|Min|Hide<br>
      UseErrorLevel</dt>
  <dd><p>If omitted, <em>Target</em> will be launched normally. Alternatively, it can contain one or more of these words:</p>
      <p><strong>Max</strong>: launch maximized</p>
      <p><strong>Min</strong>: launch minimized</p>
      <p><strong>Hide</strong>: launch hidden (cannot be used in combination with either of the above)</p>
      <p>Note: Some applications (e.g. Calc.exe) do not obey the requested startup state and thus Max/Min/Hide will have no effect.</p>
      <p><strong><a name="UseErrorLevel"></a>UseErrorLevel</strong>: UseErrorLevel can be specified alone or in addition to one of the above words (by separating it from the other word with a space). If the launch fails, this option skips the warning dialog, sets <a href="../misc/ErrorLevel.htm">ErrorLevel</a> to the word ERROR, and allows the <a href="../misc/Threads.htm">current thread</a> to continue. If the launch succeeds, RunWait sets <a href="../misc/ErrorLevel.htm">ErrorLevel</a> to the program's exit code, and  Run sets it to 0.</p>
      <p><a name="LastError"></a>When UseErrorLevel is specified, the variable <strong>A_LastError</strong> is set to the result of the operating system's GetLastError() function. A_LastError is a number between 0 and 4294967295 (always formatted as decimal, not hexadecimal). Zero (0) means success, but any other number means the launch failed. Each number corresponds to a specific error condition (to get a list, search <a href="http://www.microsoft.com">www.microsoft.com</a> for &quot;system error codes&quot;). Like <a href="../misc/ErrorLevel.htm">ErrorLevel</a>, A_LastError is a per-thread setting; that is, interruptions by other <a href="../misc/Threads.htm">threads</a> cannot change it. However, A_LastError is also set by <a href="DllCall.htm#LastError">DllCall</a>.</p></dd>

  <dt>OutputVarPID</dt>
  <dd><p>The name of the variable in which to store the newly launched program's unique <a href="Process.htm">Process ID (PID)</a>. The variable will be made blank if the PID could not be determined, which usually happens if a system verb, document, or shortcut is launched rather than a direct executable file. RunWait also supports this parameter, though its <em>OuputVarPID</em> must be checked in <a href="../misc/Threads.htm">another thread</a> (otherwise, the PID will be invalid because the process will have terminated by the time the line following RunWait executes).</p>
      <p>After the Run command retrieves a PID, any windows to be created by the process might not exist yet. To wait for at least one window to be created, use <code><a href="WinWait.htm">WinWait</a> ahk_pid %OutputVarPID%</code>.</p></dd>

</dl>
<h3>ErrorLevel</h3>
<p><span class="ver">[v1.1.04+]</span> This command is able to throw an exception on failure. For more information, see <a href="Catch.htm#RuntimeErrors">Runtime Errors</a>.</p>
<p>Run: Does not set <a href="../misc/ErrorLevel.htm">ErrorLevel</a> unless UseErrorLevel (above) is in effect, in which case ErrorLevel is set to the word ERROR upon failure or 0 upon success.</p>
<p>RunWait: Sets ErrorLevel to the program's exit code (a signed 32-bit integer). If UseErrorLevel is in effect and the launch failed, the word ERROR is stored.</p>
<h3>Remarks</h3>
<p>Unlike Run, RunWait will wait until <em>Target</em> is closed or exits, at which time <a href="../misc/ErrorLevel.htm">ErrorLevel</a> will be set to the program's exit code (as a signed 32-bit integer). Some programs will appear to return immediately even though they are still running; these programs spawn another process.</p>
<p>If <em>Target</em> contains any commas, they must be <a href="_EscapeChar.htm">escaped</a> as shown three times in the following example:</p>
<pre>Run rundll32.exe shell32.dll<span class="style3">`,</span>Control_RunDLL desk.cpl<span class="style3">`,`,</span> 3  <em>; Opens Control Panel &gt; Display Properties &gt; Settings</em></pre>
<p>When running a program via <a href="../Variables.htm#ComSpec">Comspec</a> (cmd.exe) -- perhaps because you need to redirect the program's input or output -- if the path or name of the executable contains spaces, the entire string should be enclosed in an outer pair of quotes. In the following example, the outer quotes are shown in red and all the inner quotes are shown in black:</p>
<pre>Run %comspec% /c <span class="style3">&quot;</span>&quot;C:\My Utility.exe&quot; &quot;param 1&quot; &quot;second param&quot; &gt;&quot;C:\My File.txt&quot;<span class="style3">&quot;</span></pre>
<p>If <em>Target</em> cannot be launched, an error window is displayed and the current thread is exited, unless the string <strong>UseErrorLevel</strong> is included in the third parameter or the error is caught by a <a href="Try.htm">Try</a>/<a href="Catch.htm">Catch</a> statement.</p>
<p>Performance may be slightly improved if <em>Target</em> is an exact path, e.g. <code>Run, C:\Windows\Notepad.exe &quot;C:\My Documents\Test.txt&quot;</code> rather than <code>Run, C:\My Documents\Test.txt</code>.</p>
<p>Special <a href="../misc/CLSID-List.htm">CLSID folders</a> may be opened via Run. For example:</p>
<pre>Run ::{20d04fe0-3aea-1069-a2d8-08002b30309d}  <em>; Opens the &quot;My Computer&quot; folder.</em>
Run ::{645ff040-5081-101b-9f08-00aa002f954e}  <em>; Opens the Recycle Bin.</em></pre>
<p id="verbs">System verbs correspond to actions available in a file's right-click menu in the Explorer. If a file is launched without a verb, the default verb (usually &quot;open&quot;) for that particular file type will be used. If specified, the verb should be followed by the name of the target file. The following verbs are currently supported:</p>
<table class="info">
  <tr>
    <td width="8%">*<i>verb</i></td>
    <td width="92%"><span class="ver">[AHK_L 57+]:</span> Any system-defined or custom verb. For example: <code>Run *Compile %A_ScriptFullPath%</code><br>On Windows Vista and later, the <code>*RunAs</code> verb may be used in place of the <i>Run As Administrator</i> right-click menu item.</td>
  </tr>
  <tr>
    <td>properties</td>
    <td>Displays the Explorer's properties window for the indicated file. For example: <code>Run, properties &quot;C:\My File.txt&quot;</code><br>
    Note: The properties window will automatically close when the script terminates. To prevent this, use <a href="WinWait.htm">WinWait</a> to wait for the window to appear, then use <a href="WinWaitClose.htm">WinWaitClose</a> to wait for the user to close it.</td>
  </tr>
  <tr>
    <td>find</td>
    <td>Opens an instance of the Explorer's Search Companion or Find File window at the indicated folder. For example: <code>Run, find D:\</code></td>
  </tr>
  <tr>
    <td>explore</td>
    <td>Opens an instance of Explorer at the indicated folder. For example: <code>Run, explore %A_ProgramFiles%</code>.</td>
  </tr>
  <tr>
    <td>edit</td>
    <td>Opens the indicated file for editing. It might not work if the indicated file's type does not have an &quot;edit&quot; action associated with it. For example: <code>Run, edit &quot;C:\My File.txt&quot;</code></td>
  </tr>
  <tr>
    <td>open</td>
    <td>Opens the indicated file (normally not needed because it is the default action for most file types). For example: <code>Run, open &quot;My File.txt&quot;</code>.</td>
  </tr>
  <tr>
    <td>print</td>
    <td>Prints the indicated file with the associated application, if any. For example: <code>Run, print &quot;My File.txt&quot;</code></td>
  </tr>
</table>
<p>While RunWait is in a waiting state, new <a href="../misc/Threads.htm">threads</a> can be launched via <a href="../Hotkeys.htm">hotkey</a>, <a href="Menu.htm">custom menu item</a>, or <a href="SetTimer.htm">timer</a>.</p>
<h3>Related</h3>
<p><a href="RunAs.htm">RunAs</a>, <a href="Process.htm">Process</a>, <a href="Exit.htm">Exit</a>, <a href="../misc/CLSID-List.htm">CLSID List</a>, <a href="DllCall.htm">DllCall</a></p>
<h3>Examples</h3>
<pre class="NoIndent">Run, Notepad.exe, C:\My Documents, max

Run, mailto:someone@domain.com?subject=This is the subject line&amp;body=This is the message body's text.
Run, ReadMe.doc, , Max UseErrorLevel  <em>; Launch maximized and don't display dialog if it fails.</em>
if ErrorLevel = ERROR
    MsgBox The document could not be launched.

RunWait, %comspec% /c dir c:\ &gt;&gt;c:\DirTest.txt, , min
Run, c:\DirTest.txt
Run, properties c:\DirTest.txt

Run, http://www.google.com <em>; i.e. any URL can be launched.</em>
Run, mailto:someone@somedomain.com  <em>; This should open the default e-mail application.</em>

Run ::{20d04fe0-3aea-1069-a2d8-08002b30309d}  <em>; Opens the &quot;My Computer&quot; folder.</em>
Run ::{645ff040-5081-101b-9f08-00aa002f954e}  <em>; Opens the Recycle Bin.</em>

<em>; To run multiple commands consecutively, use &quot;&amp;&amp;&quot; between each:</em>
Run, %comspec% /c dir /b &gt; C:\list.txt &amp;&amp; type C:\list.txt &amp;&amp; pause
</pre>

<pre class="NoIndent" id="StdOut">
<em>; The following can be used to run a command and retrieve its output:</em>
MsgBox % RunWaitOne("dir " A_ScriptDir)

<em>; ...or run multiple commands in one go and retrieve their output:</em>
MsgBox % RunWaitMany("
(
echo Put your commands here,
echo each one will be run,
echo and you'll get the output.
)")

RunWaitOne(command) {
    <em>; WshShell object: <a href="http://msdn.microsoft.com/en-us/library/aew9yb99">http://msdn.microsoft.com/en-us/library/aew9yb99</a></em>
    shell := ComObjCreate("WScript.Shell")
    <em>; Execute a single command via cmd.exe</em>
    exec := shell.Exec(ComSpec " /C " command)
    <em>; Read and return the command's output</em>
    return exec.StdOut.ReadAll()
}

RunWaitMany(commands) {
    shell := ComObjCreate("WScript.Shell")
    <em>; Open cmd.exe with echoing of commands disabled</em>
    exec := shell.Exec(ComSpec " /Q /K echo off")
    <em>; Send the commands to execute, separated by newline</em>
    exec.StdIn.WriteLine(commands "`nexit")  <em>; Always exit at the end!</em>
    <em>; Read and return the output of all commands</em>
    return exec.StdOut.ReadAll()
}
</pre>

<pre class="NoIndent" id="ExecScript">
<em>; ExecScript: Executes the given code as a new AutoHotkey process.</em>
ExecScript(Script, Wait:=true)
{
    shell := ComObjCreate("WScript.Shell")
    exec := shell.Exec("AutoHotkey.exe /ErrorStdOut *")
    exec.StdIn.Write(script)
    exec.StdIn.Close()
    if Wait
        return exec.StdOut.ReadAll()
}

<em>; Example:</em>
InputBox expr,, Enter an expression to evaluate as a new script.,,,,,,,, Asc("*")
result := ExecScript("FileAppend % (" expr "), *")
MsgBox % "Result: " result
</pre>

</body>
</html>
